home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / pyshared / parted / device.py < prev    next >
Encoding:
Python Source  |  2010-06-29  |  11.8 KB  |  341 lines

  1. #
  2. # device.py
  3. # Python bindings for libparted (built on top of the _ped Python module).
  4. #
  5. # Copyright (C) 2009  Red Hat, Inc.
  6. #
  7. # This copyrighted material is made available to anyone wishing to use,
  8. # modify, copy, or redistribute it subject to the terms and conditions of
  9. # the GNU General Public License v.2, or (at your option) any later version.
  10. # This program is distributed in the hope that it will be useful, but WITHOUT
  11. # ANY WARRANTY expressed or implied, including the implied warranties of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
  13. # Public License for more details.  You should have received a copy of the
  14. # GNU General Public License along with this program; if not, write to the
  15. # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. # 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
  17. # source code or documentation are not subject to the GNU General Public
  18. # License and may only be used or replicated with the express permission of
  19. # Red Hat, Inc.
  20. #
  21. # Red Hat Author(s): David Cantrell <dcantrell@redhat.com>
  22. #
  23.  
  24. import math
  25. import _ped
  26. import parted
  27. from disk import diskType
  28.  
  29. from decorators import localeC
  30.  
  31. class Device(object):
  32.     """Device()
  33.  
  34.        Device represents a phyiscal piece of hardware in the system, e.g. a
  35.        disk.  A Device should be considered a low-level and operating system
  36.        specific interface to the hardware.
  37.  
  38.        A number of read-only properties about the Device are available.
  39.  
  40.        For information on the individual methods, see help(Device.METHODNAME)"""
  41.  
  42.     @localeC
  43.     def __init__(self, path=None, PedDevice=None):
  44.         """Create a new Device object based on the specified path or the
  45.            already existing _ped.Device object.  You must provide either a
  46.            path (e.g., "/dev/sda") or an existing _ped.Device object, but
  47.            not both."""
  48.  
  49.         if PedDevice:
  50.             self.__device = PedDevice
  51.         elif path is not None:
  52.             self.__device = _ped.device_get(path)
  53.         else:
  54.             raise parted.DeviceException, "no path or PedDevice specified"
  55.  
  56.     def __eq__(self, other):
  57.         return not self.__ne__(other)
  58.  
  59.     def __ne__(self, other):
  60.         if hash(self) == hash(other):
  61.             return False
  62.  
  63.         if type(self) != type(other):
  64.             return True
  65.  
  66.         return self.model != other.model or self.path != other.path or self.type != other.type or self.length != other.length
  67.  
  68.     def __getCHS(self, geometry):
  69.         return (geometry.cylinders, geometry.heads, geometry.sectors)
  70.  
  71.     @property
  72.     def model(self):
  73.         """Model name and vendor of this device."""
  74.         return self.__device.model
  75.  
  76.     @property
  77.     def path(self):
  78.         """Filesystem node path of this device (e.g., /dev/sda)."""
  79.         return self.__device.path
  80.  
  81.     @property
  82.     def type(self):
  83.         """Type of this device.  An integer constant corresponding
  84.            to one of the parted.DEVICE_* values.
  85.         """
  86.         return self.__device.type
  87.  
  88.     @property
  89.     def sectorSize(self):
  90.         """Sector size (in bytes) for this device."""
  91.         return self.__device.sector_size
  92.  
  93.     @property
  94.     def physicalSectorSize(self):
  95.         """Physical sector size (in bytes) for this device.  Not always
  96.            the same as sectorSize, but is a multiple of sectorSize.
  97.         """
  98.         return self.__device.phys_sector_size
  99.  
  100.     @property
  101.     def length(self):
  102.         """The size of this device in sectors."""
  103.         return self.__device.length
  104.  
  105.     @property
  106.     def openCount(self):
  107.         """How many times the open() method has been called on this device."""
  108.         return self.__device.open_count
  109.  
  110.     @property
  111.     def readOnly(self):
  112.         """True if the device is currently in read-only mode, False
  113.            otherwise.
  114.         """
  115.         return bool(self.__device.read_only)
  116.  
  117.     @property
  118.     def externalMode(self):
  119.         """True if external access mode is currently activated on this
  120.            device, False otherwise.  External access mode has to be used
  121.            if you want to use an external command on the device while
  122.            you are currently using it in pyparted.
  123.         """
  124.         return bool(self.__device.external_mode)
  125.  
  126.     @property
  127.     def dirty(self):
  128.         """True if the device is marked dirty, False otherwise."""
  129.         return bool(self.__device.dirty)
  130.  
  131.     @property
  132.     def bootDirty(self):
  133.         """True if the device is marked boot dirty, False otherwise."""
  134.         return bool(self.__device.boot_dirty)
  135.  
  136.     @property
  137.     def host(self):
  138.         """The host value of this device."""
  139.         return self.__device.host
  140.  
  141.     @property
  142.     def did(self):
  143.         """The did value of this device."""
  144.         return self.__device.did
  145.  
  146.     @property
  147.     def busy(self):
  148.         """True if this device is busy, False otherwise."""
  149.         return self.__device.is_busy()
  150.  
  151.     @property
  152.     def hardwareGeometry(self):
  153.         """A 3-tuple representing the hardware geometry of this device.
  154.            The tuple is in order of cylinders, heads, and sectors.
  155.         """
  156.         return self.__getCHS(self.__device.hw_geom)
  157.  
  158.     @property
  159.     def biosGeometry(self):
  160.         """A 3-tuple representing the BIOS geometry of this device.
  161.            The tuple is in order of cylinders, heads, and sectors.
  162.         """
  163.         return self.__getCHS(self.__device.bios_geom)
  164.  
  165.     def __str__(self):
  166.         s = ("parted.Device instance --\n"
  167.              "  model: %(model)s  path: %(path)s  type: %(type)s\n"
  168.              "  sectorSize: %(sectorSize)s  physicalSectorSize:  %(physSectorSize)s\n"
  169.              "  length: %(length)s  openCount: %(openCount)s  readOnly: %(readOnly)s\n"
  170.              "  externalMode: %(external)s  dirty: %(dirty)s  bootDirty: %(bootDirty)s\n"
  171.              "  host: %(host)s  did: %(did)s  busy: %(busy)s\n"
  172.              "  hardwareGeometry: %(hardwareGeom)s  biosGeometry: %(biosGeom)s\n"
  173.              "  PedDevice: %(ped)r" %
  174.              {"model": self.model, "path": self.path, "type": self.type,
  175.               "sectorSize": self.sectorSize, "physSectorSize": self.physicalSectorSize,
  176.               "length": self.length, "openCount": self.openCount, "readOnly": self.readOnly,
  177.               "external": self.externalMode, "dirty": self.dirty, "bootDirty": self.bootDirty,
  178.               "host": self.host, "did": self.did, "busy": self.busy,
  179.               "hardwareGeom": self.hardwareGeometry, "biosGeom": self.biosGeometry,
  180.               "ped": self.__device})
  181.         return s
  182.  
  183.     @localeC
  184.     def clobber(self):
  185.         """Remove all identifying signatures of the partition table."""
  186.         return self.__device.clobber()
  187.  
  188.     @localeC
  189.     def open(self):
  190.         """Open this Device for read operations."""
  191.  
  192.         return self.__device.open()
  193.  
  194.     @localeC
  195.     def close(self):
  196.         """Close this Device.  Used after open() method calls."""
  197.  
  198.         return self.__device.close()
  199.  
  200.     @localeC
  201.     def destroy(self):
  202.         """Destroy this Device.  Operating system specific."""
  203.  
  204.         return self.__device.destroy()
  205.  
  206.     @localeC
  207.     def removeFromCache(self):
  208.         """Remove this Device from the internal libparted device cache."""
  209.  
  210.         return self.__device.cache_remove()
  211.  
  212.     @localeC
  213.     def beginExternalAccess(self):
  214.         """Set up the Device for use by an external program.  Call this method
  215.            before running an external program that uses the Device."""
  216.  
  217.         return self.__device.begin_external_access()
  218.  
  219.     @localeC
  220.     def endExternalAccess(self):
  221.         """Turn off external access mode for the Device.  Call this method once
  222.            your external program has finished using the Device."""
  223.  
  224.         return self.__device.end_external_access()
  225.  
  226.     @localeC
  227.     def read(self, start, count):
  228.         """From the sector indentified by start, read and return count sectors
  229.            from the Device."""
  230.  
  231.         return self.__device.read(start, count)
  232.  
  233.     @localeC
  234.     def write(self, buffer, start, count):
  235.         """From the sector identified by start, write count sectors from
  236.            buffer to the Device."""
  237.  
  238.         return self.__device.write(buffer, start, count)
  239.  
  240.     @localeC
  241.     def sync(self, fast=False):
  242.         """Perform a operating-system specific sync(2) operation on the
  243.            Device.  If fast is True, try to perform a fast sync(2)."""
  244.  
  245.         if fast:
  246.             return self.__device.sync_fast()
  247.         else:
  248.             return self.__device.sync()
  249.  
  250.     @localeC
  251.     def check(self, start, count):
  252.         """From the sector identified by start, perform an operating
  253.            system specific check on count sectors."""
  254.         return self.__device.check(start, count)
  255.  
  256.     @localeC
  257.     def startSectorToCylinder(self, sector):
  258.         """Return the closest cylinder (round down) to sector on
  259.            this Device."""
  260.         (cylinders, heads, sectors) = self.biosGeometry
  261.         return long(math.floor((float(sector) / (heads * sectors)) + 1))
  262.  
  263.     @localeC
  264.     def endSectorToCylinder(self, sector):
  265.         """Return the closest cylinder (round up) to sector on
  266.            this Device."""
  267.         (cylinders, heads, sectors) = self.biosGeometry
  268.         return long(math.ceil(float((sector + 1)) / (heads * sectors)))
  269.  
  270.     @localeC
  271.     def startCylinderToSector(self, cylinder):
  272.         """Return the sector corresponding to cylinder as a
  273.            starting cylinder on this Device."""
  274.         (cylinders, heads, sectors) = self.biosGeometry
  275.         return long((cylinder - 1) * (heads * sectors))
  276.  
  277.     @localeC
  278.     def endCylinderToSector(self, cylinder):
  279.         """Return the sector corresponding to cylinder as an
  280.            ending cylinder on this Device."""
  281.         (cylinders, heads, sectors) = self.biosGeometry
  282.         return long(((cylinder) * (heads * sectors)) - 1)
  283.  
  284.     def getSize(self, unit="MB"):
  285.         """Return the size of the Device in the unit specified.  The unit
  286.            is given as a string corresponding to one of the following
  287.            abbreviations:  b (bytes), KB (kilobytes), MB (megabytes), GB
  288.            (gigabytes), TB (terabytes).  An invalid unit string will raise a
  289.            SyntaxError exception.  The default unit is MB."""
  290.         lunit = unit.lower()
  291.  
  292.         if lunit not in parted._exponent.keys():
  293.             raise SyntaxError, "invalid unit %s given" % (unit,)
  294.  
  295.         size = float(self.__device.length)
  296.         size /= math.pow(1024.0, parted._exponent[lunit])
  297.         size *= self.sectorSize
  298.  
  299.         return size
  300.  
  301.     @localeC
  302.     def getConstraint(self):
  303.         """Return a Constraint defining the limitations imposed by
  304.            this Device."""
  305.         return parted.Constraint(PedConstraint=self.__device.get_constraint())
  306.  
  307.     @property
  308.     @localeC
  309.     def minimalAlignedConstraint(self):
  310.         """Return a Constraint defining the limitations and minimal advisable
  311.            alignment imposed by this Device."""
  312.         constraint = self.__device.get_minimal_aligned_constraint()
  313.         return parted.Constraint(PedConstraint=constraint)
  314.  
  315.     @property
  316.     @localeC
  317.     def optimalAlignedConstraint(self):
  318.         """Return a Constraint defining the limitations and optimal
  319.            alignment imposed by this Device."""
  320.         constraint = self.__device.get_optimal_aligned_constraint()
  321.         return parted.Constraint(PedConstraint=constraint)
  322.  
  323.     @property
  324.     @localeC
  325.     def minimumAlignment(self):
  326.         """Return an Alignment defining the minimum alignment for this Device."""
  327.         alignment = self.__device.get_minimum_alignment()
  328.         return parted.Alignment(PedAlignment=alignment)
  329.  
  330.     @property
  331.     @localeC
  332.     def optimumAlignment(self):
  333.         """Return an Alignment defining the optimum alignment for this Device."""
  334.         alignment = self.__device.get_optimum_alignment()
  335.         return parted.Alignment(PedAlignment=alignment)
  336.  
  337.     def getPedDevice(self):
  338.         """Return the _ped.Device object contained in this Device.
  339.            For internal module use only."""
  340.         return self.__device
  341.